/**
 * 
 */
package com.ims.stocks.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import com.ims.common.action.BaseAction;
import com.ims.common.utils.CodeValueData;
import com.ims.common.utils.propertyManager;
import com.ims.stocks.bo.VendorBO;
import com.ims.stocks.model.Employee;
import com.ims.stocks.model.Vendor;
import com.ims.stocks.service.address.AddressService;
import com.ims.stocks.service.person.PersonService;
import com.ims.stocks.service.vendor.VendorService;

/**
 * @author Achal B.I
 *
 */
public class VendorAction extends BaseAction implements ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7643701038758833988L;
	
	private Vendor vendor = new Vendor();
	private String crudType; 
	private VendorService vendorService;
	private PersonService personService;
	private AddressService addressService;
	private HttpServletRequest request;
	private List<Vendor> vendorsList = new ArrayList<Vendor>();
	private List<VendorBO> vendorBOsList = new ArrayList<VendorBO>();
	private List<CodeValueData> vendorHeaderList = new ArrayList<CodeValueData>();
	
	private List<CodeValueData> shiftList = new ArrayList<CodeValueData>();
	private List<CodeValueData> genderList = new ArrayList<CodeValueData>();
	private List<CodeValueData> statusList = new ArrayList<CodeValueData>();


	private final String SHIFT = "common.employee.shift";
	private final String GENDER = "common.person.gender";
	private final String VENDOR_HEADER = "common.vendor.header";
	private final String STATUS = "common.user.status";

	public String execute(){
		loadAction();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private void loadAction() {
		vendorHeaderList = propertyManager.getInstance().getListFromProp(VENDOR_HEADER);
		genderList = propertyManager.getInstance().getListFromProp(GENDER);
		shiftList = propertyManager.getInstance().getListFromProp(SHIFT);
		statusList = propertyManager.getInstance().getListFromProp(STATUS);
		
		String Id = getServletRequest().getParameter("id");
		setCrudType(getServletRequest().getParameter("pType"));
		if(Id!=null&&!Id.isEmpty()){
			vendor = (Vendor) vendorService.findById(Employee.class, Integer.parseInt(Id) );
		}
		if(crudType!=null && crudType.equalsIgnoreCase("Delete")){
			vendorService.txDelete(vendor);
			addActionMessage("Record deleted successfully");
		}
		vendorsList = vendorService.findAll(Employee.class);
		for (Vendor ven : vendorsList) {
			//vendorBOsList.add(new EmployeeBO(ven, emp.getShift(), emp.getPerson().getGender(), emp.getStatus()));
		}

	}

	@SuppressWarnings("unchecked")
	public String addEmployee() throws Exception{
/*		Address address = (Address) employeeService.findById(Address.class, employee.getPerson().getAddress().getId());	
		user.setRole(role);
		if(crudType!=null && crudType.equalsIgnoreCase("New"))
		{
			user.setCreationDate(new Date());
		}
		if(crudType!=null && crudType.equalsIgnoreCase("Edit"))
		{
			user.setUpdationDate(new Date());
		}*/

		vendorService.txSaveOrUpdateObject(vendor);

		return SUCCESS;
 
	}
	



	public List<CodeValueData> getShiftList() {
		return shiftList;
	}

	public void setShiftList(List<CodeValueData> shiftList) {
		this.shiftList = shiftList;
	}

	/**
	 * @param genderList the genderList to set
	 */
	public void setGenderList(List<CodeValueData> genderList) {
		this.genderList = genderList;
	}

	/**
	 * @return the genderList
	 */
	public List<CodeValueData> getGenderList() {
		return genderList;
	}

	/**
	 * @param statusList the statusList to set
	 */
	public void setStatusList(List<CodeValueData> statusList) {
		this.statusList = statusList;
	}

	/**
	 * @return the statusList
	 */
	public List<CodeValueData> getStatusList() {
		return statusList;
	}

	public HttpServletRequest getServletRequest(){
		return request;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	/**
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return request;
	}
	

	/**
	 * @param request the request to set
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * @return the crudType
	 */
	public String getCrudType() {
		return crudType;
	}

	/**
	 * @param crudType the crudType to set
	 */
	public void setCrudType(String crudType) {
		this.crudType = crudType;
	}


	/**
	 * @return the personService
	 */
	public PersonService getPersonService() {
		return personService;
	}

	/**
	 * @param personService the personService to set
	 */
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	/**
	 * @return the addressService
	 */
	public AddressService getAddressService() {
		return addressService;
	}

	/**
	 * @param addressService the addressService to set
	 */
	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	/**
	 * @param vendor the vendor to set
	 */
	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}

	/**
	 * @return the vendor
	 */
	public Vendor getVendor() {
		return vendor;
	}

	/**
	 * @return the vendorService
	 */
	public VendorService getVendorService() {
		return vendorService;
	}

	/**
	 * @param vendorService the vendorService to set
	 */
	public void setVendorService(VendorService vendorService) {
		this.vendorService = vendorService;
	}

	/**
	 * @return the vendorsList
	 */
	public List<Vendor> getVendorsList() {
		return vendorsList;
	}

	/**
	 * @param vendorsList the vendorsList to set
	 */
	public void setVendorsList(List<Vendor> vendorsList) {
		this.vendorsList = vendorsList;
	}

	/**
	 * @return the vendorBOsList
	 */
	public List<VendorBO> getVendorBOsList() {
		return vendorBOsList;
	}

	/**
	 * @param vendorBOsList the vendorBOsList to set
	 */
	public void setVendorBOsList(List<VendorBO> vendorBOsList) {
		this.vendorBOsList = vendorBOsList;
	}

	/**
	 * @return the vendorHeaderList
	 */
	public List<CodeValueData> getVendorHeaderList() {
		return vendorHeaderList;
	}

	/**
	 * @param vendorHeaderList the vendorHeaderList to set
	 */
	public void setVendorHeaderList(List<CodeValueData> vendorHeaderList) {
		this.vendorHeaderList = vendorHeaderList;
	}

	/**
	 * @return the vENDOR_HEADER
	 */
	public String getVENDOR_HEADER() {
		return VENDOR_HEADER;
	}

}
